package com.smec.moblie.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.smec.moblie.R;
import com.smec.moblie.crash.ExitAppUtils;

import java.util.List;

import me.zhanghai.android.patternlock.PatternUtils;
import me.zhanghai.android.patternlock.PatternView;

/**
 * 手势密码界面
 * Created by sunyubo on 16/3/10.
 */
public class ConfirmPatternActivity extends me.zhanghai.android.patternlock.ConfirmPatternActivity {
    private SharedPreferences sharedPreferences;
    /** 错误计数器 **/
    private int errTemp = 0;
    /** 是否为重置手势密码 **/
    private boolean isChangePatter = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.PatterAppTheme);
        sharedPreferences = getSharedPreferences(
                "ElevatorSharedPreferences", SetPatternActivity.MODE_PRIVATE);
        isChangePatter = getIntent().getBooleanExtra("isChangePatter", false);
        ExitAppUtils.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected boolean isStealthModeEnabled() {

        return false;
    }

    @Override
    protected boolean isPatternCorrect(List<PatternView.Cell> pattern) {
        String patternSha1 = sharedPreferences.getString("patternSha1", "");
        boolean isTrue = TextUtils.equals(PatternUtils.patternToSha1String(pattern), patternSha1);
        if (isTrue) {
            if(isChangePatter){
                Intent intent = new Intent(ConfirmPatternActivity.this, SetPatternActivity.class);
                intent.putExtra("isChangePatter",isChangePatter);
                startActivity(intent);
            }else {
                Intent intent = new Intent(ConfirmPatternActivity.this, MainActivity.class);
                startActivity(intent);
            }
        } else {
            errTemp++;
            if(errTemp<5) {
                Toast.makeText(this, "当前手势错误,剩余" + (5 - errTemp) + "次机会", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "错误次数太多，请重新输入账号密码", Toast.LENGTH_SHORT).show();
            }
        }
        if (errTemp >= 5) {//5次错误后清除手势密码，重新登入
            SharedPreferences.Editor editor = sharedPreferences
                    .edit();
            editor.putBoolean("isSetPassWord", false);
            // 提交当前数据
            editor.commit();

            startActivity(new Intent(this, LoginActivity.class));
            ConfirmPatternActivity.this.finish();
        }
        return isTrue;
    }

    @Override
    protected void onForgotPassword() {
        SharedPreferences.Editor editor = sharedPreferences
                .edit();
        editor.putBoolean("isSetPassWord", false);
        // 提交当前数据
        editor.commit();

        startActivity(new Intent(this, LoginActivity.class));

        // Finish with RESULT_FORGOT_PASSWORD.
        super.onForgotPassword();
    }

}
